import {ref} from 'vue'

function padNum(num: number): string {
    return num < 10 ? '0' + num : '' + num
}

const nowTime = ref('00:00:00')

let timer: number | undefined

const updateNowTime = () => {
    clearTimeout(timer)
    const now = new Date()
    const hour = padNum(now.getHours())
    const min = padNum(now.getMinutes())
    const sec = padNum(now.getSeconds())

    nowTime.value = `${hour}:${min}:${sec}`

    timer = setTimeout(updateNowTime, 1000)
}

export {
    nowTime,
    updateNowTime
}
